import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        String s = "AABCDC";
        String res = solution(s);
        System.out.println(res);
    }

    public static String solution(String s){
        Stack<Character> stack = new Stack<>();
        char[] ch = s.toCharArray();
        stack.push(ch[0]);
        for(int i = 1 ; i < ch.length ; i++){
            if(stack.isEmpty()){
                stack.push(ch[i]);
                continue;
            }
            //将字符与栈顶比较匹配，如果能消去就出栈，如果不能就入栈。
            //如果栈为空，就直接入栈
            //与“判断有效的括号”相似
            if(ch[i] == 'A'){
                char tmp = stack.peek();
                if(tmp == 'B'){
                    stack.pop();
                }else{
                    stack.push(ch[i]);
                }
            }
            if(ch[i] == 'B'){
                char tmp = stack.peek();
                if(tmp == 'A'){
                    stack.pop();
                }else{
                    stack.push(ch[i]);
                }
            }
            if(ch[i] == 'C'){
                char tmp = stack.peek();
                if(tmp == 'D'){
                    stack.pop();
                }else{
                    stack.push(ch[i]);
                }
            }
            if(ch[i] == 'D'){
                char tmp = stack.peek();
                if(tmp == 'C'){
                    stack.pop();
                }else{
                    stack.push(ch[i]);
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }

}
